/**
 * 
 */
package com.uminho.objects;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.lang.String;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.uminho.egendme.R;
import com.uminho.tools.ApplicationContext;

import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;

/****c* Objects/Profile
* DESCRIPTION
* Objeto abstrato utilizado para armazenar informação relativa a perfis. Ele é utilizado nos objetos Service e User.
****
*/
public abstract class Profile implements Serializable{
	Long id;
	String username, name, email, phone, mobile_phone, address, website;
	Character gender;
	Date birthday;
	Drawable avatar; // http://stackoverflow.com/questions/4223472/how-to-display-image-from-internet-in-android
	
	public Profile(Long id, String username, String name, String email,
			String phone, String mobile_phone, String website,
			Character gender, String address, Date birthday, Drawable avatar) {
		this.id = id;
		this.username = username;
		this.name = name;
		this.email = email;
		this.phone = phone;
		this.mobile_phone = mobile_phone;
		this.website = website;
		this.gender = gender;
		this.address = address;
		this.birthday = birthday;
		this.avatar = avatar;
	}

	public Profile(Long id, String username, String name, String email,
			String phone, String mobile_phone, String website,
			Character gender, String address) {
		this.id = id;
		this.username = username;
		this.name = name;
		this.email = email;
		this.phone = phone;
		this.mobile_phone = mobile_phone;
		this.website = website;
		this.gender = gender;
		this.address = address;
	}

	public Profile() {
	}

	// GETTERS & SETTERS

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setId(String id) {
		this.id = Long.parseLong(id);
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getMobile_phone() {
		return mobile_phone;
	}

	public void setMobile_phone(String mobile_phone) {
		this.mobile_phone = mobile_phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getWebsite() {
		return website;
	}

	public void setWebsite(String website) {
		this.website = website;
	}

	public Character getGender() {
		return gender;
	}

	public void setGender(Character gender) {
		this.gender = gender;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public void setBirthday(String birthday) throws ParseException {
		if (birthday == null || birthday.length() == 0) {
			birthday = null;
		} else {
			DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
			Date birth;
			birth = dfm.parse(birthday);
			this.birthday = birth;
		}
	}

	public Drawable getAvatar() {
		return avatar;
	}

	public void setAvatar(Drawable avatar) {
		this.avatar = avatar;
	}

	
	abstract void setAvatar() throws MalformedURLException,
			IOException;

	// http://www.java2s.com/Code/Android/2D-Graphics/DrawablecreateFromStream.htm
	protected InputStream fetch(String urlString) throws MalformedURLException,
			IOException {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpGet request = new HttpGet(urlString);
		HttpResponse response = httpClient.execute(request);
		return response.getEntity().getContent();
	}
	
	protected String displaySize(){
		DisplayMetrics dm = ApplicationContext.c.getResources().getDisplayMetrics();

		switch(dm.densityDpi){
		     case DisplayMetrics.DENSITY_LOW:
		                return "ldpi";
		     case DisplayMetrics.DENSITY_MEDIUM:
		    	 		return "mdpi";
		     case DisplayMetrics.DENSITY_HIGH:
		    	 		return "hdpi";
		}
		return "hdpi";
	}
	
	/**
	  * Custom deserialization is needed.
	  */
		
	  private void readObject(
	    ObjectInputStream aStream
	  ) throws IOException, ClassNotFoundException {
	    //manually deserialize and init superclass
		this.avatar= ApplicationContext.c.getResources().getDrawable(R.drawable.empty_avatar);
	    this.id=(Long)aStream.readObject();
	    this.username=(String)aStream.readObject();
	    this.name=(String)aStream.readObject();
	    this.email=(String)aStream.readObject();
	    this.phone=(String)aStream.readObject();
	    this.mobile_phone=(String)aStream.readObject();
	    this.address=(String)aStream.readObject();
	    this.website=(String)aStream.readObject();
	    this.gender=(Character)aStream.readObject();
	    this.birthday=(Date)aStream.readObject();
	  }

	  /**
	  * Custom serialization is needed.
	  */
	  private void writeObject(ObjectOutputStream aStream) throws IOException {
	    aStream.writeObject(this.id);
	    aStream.writeObject(this.username);
	    aStream.writeObject(this.name);
	    aStream.writeObject(this.email);
	    aStream.writeObject(this.phone);
	    aStream.writeObject(this.mobile_phone);
	    aStream.writeObject(this.address);
	    aStream.writeObject(this.website);
	    aStream.writeObject(this.gender);
	    aStream.writeObject(this.birthday);
	  }
}
